package com.dms_back1.controller;

import com.dms_back1.annotation.RateLimit;
import com.dms_back1.bean.enums.VerifyCodeType;
import com.dms_back1.bean.req.LoginReq;
import com.dms_back1.bean.req.RegisterReq;
import com.dms_back1.bean.req.ThirdPartyLoginReq;
import com.dms_back1.bean.res.LoginRes;
import com.dms_back1.bean.res.Result;
import com.dms_back1.ex.BusinessException;
import com.dms_back1.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.concurrent.TimeUnit;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    
    @Operation(summary = "发送验证码")
    @RateLimit(key = "send_code", time = 1, unit = TimeUnit.MINUTES, count = 1)
    @PostMapping("/send-code")
    public Result<Void> sendVerificationCode(@Validated @RequestBody SendCodeReq req) {
        userService.sendVerificationCode(req.getPhoneNumber(), req.getType());
        return Result.success();
    }
    
    @Operation(summary = "用户注册")
    @RateLimit(key = "register", time = 1, unit = TimeUnit.MINUTES, count = 3)
    @PostMapping("/register")
    public Result<Void> register(@Validated @RequestBody RegisterReq req) {
        userService.register(req);
        return Result.success();
    }
    
    @Operation(summary = "用户登录")
    @RateLimit(key = "login", time = 1, unit = TimeUnit.MINUTES, count = 5)
    @PostMapping("/login")
    public Result<LoginRes> login(@Validated @RequestBody LoginReq req) {
        LoginRes res = userService.login(req);
        return Result.success(res);
    }
    
    @Operation(summary = "第三方登录")
    @RateLimit(key = "third_party_login", time = 1, unit = TimeUnit.MINUTES, count = 5)
    @PostMapping("/third-party-login")
    public Result<LoginRes> thirdPartyLogin(@Validated @RequestBody ThirdPartyLoginReq req) {
        LoginRes res = userService.thirdPartyLogin(req);
        return Result.success(res);
    }
    
    @Operation(summary = "绑定手机号")
    @RateLimit(key = "bind_phone", time = 1, unit = TimeUnit.MINUTES, count = 3)
    @PostMapping("/bind-phone")
    public Result<Void> bindPhoneNumber(
            @RequestHeader("X-User-Id") Long userId,
            @Validated @RequestBody BindPhoneReq req) {
        userService.bindPhoneNumber(userId, req.getPhoneNumber(), req.getCode());
        return Result.success();
    }

    @Data
    public static class SendCodeReq {
        @NotBlank(message = "手机号不能为空")
        @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
        private String phoneNumber;
        
        private VerifyCodeType type = VerifyCodeType.REGISTER;  // 默认为注册类型
    }
    
    @Data
    public static class BindPhoneReq {
        @NotBlank(message = "手机号不能为空")
        @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
        private String phoneNumber;
        
        @NotBlank(message = "验证码不能为空")
        private String code;
    }
} 